<?php

namespace App\Http\Controllers\Admin;

use App\Services\Admin\NoticeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class NoticeController extends AdminBaseController
{
    protected $noticeService;

    public function __construct(NoticeService $noticeService)
    {
        $this->noticeService = $noticeService;
    }

    /**
     * 获取未读消息列表
     */
    public function unreadList(Request $request): \Illuminate\Http\JsonResponse
    {
        $params = $request->all();
        $adminId = auth()->id();
        $data = $this->noticeService->getUnreadList($adminId, $params);
        return $this->success($data);
    }
    
    /**
     * 标记消息为已读
     */
    public function markAsRead(int $id): \Illuminate\Http\JsonResponse
    {
        $result = $this->noticeService->markAsRead($id);
        return $this->success($result);
    }
    
    /**
     * 批量标记消息为已读
     */
    public function markMultipleAsRead(Request $request): \Illuminate\Http\JsonResponse
    {
        $validator = Validator::make($request->all(), [
            'ids' => 'required|array',
            'ids.*' => 'integer'
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), 422);
        }

        $ids = $request->input('ids');
        $result = $this->noticeService->markMultipleAsRead($ids);
        return $this->success($result);
    }
}